package czk.concrent.juc.countDOwnLatch;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 练习二
 *
 * @Author:ChenZhangKun
 * @Date: 2021/5/12 23:00
 */
public class CountDownAndLatchTest2 {
    public static void main(String[] args) throws InterruptedException {
        // 创建技术器
        CountDownLatch countDownLatch = new CountDownLatch(20);
        // 创建线程组
        ExecutorService threadPool = Executors.newFixedThreadPool(20);
        // 创建数组
        String[] all = new String[20];
        // 循环
        for (int i = 0; i < 20; i++) {
            // 遍历拷贝
            int k = i;
            threadPool.submit(() -> {
                // 虚幻
                for (int j = 0; j <= 100; j++) {
                    // 休眠
                    try {
                        Thread.sleep(ThreadLocalRandom.current().nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[k] = j + "%";
                    // 打印数组
                    System.out.print("\r" + Arrays.toString(all));
                    System.out.println();
                }
                // 计数器减一
                countDownLatch.countDown();
            });

        }
        // 阻塞
        countDownLatch.await();
        System.out.println("启动成功");
    }
}
